package com.wc.alorithm_blue_bridge._DFS.BlueAI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/31 17:49
 * @description
 * https://www.lanqiao.cn/problems/20100/learning/?contest_id=231
 */
public class Main {
    /**
     * 思路：模拟DFS就好了
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 15;
    static char[][] g = new char[N][N];
    static int[] dx = {-1, -1, 1, 1}, dy = {-1, 1, 1, -1};
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) g[i] = (" " + sc.next()).toCharArray();
        int res = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (g[i][j] == 'L') res = Math.max(res, dfs(i, j));
            }
        }
        out.println(res);
        out.flush();
    }

    static int dfs(int x, int y) {
        int res = 0;

        for (int i = 0; i < 4; i++) {
            int a1 = x + dx[i], b1 = y + dy[i];
            int a2 = x + 2 * dx[i], b2 = y + 2 * dy[i];
            if (a1 < 1 || a1 > n || a2 < 1 || a2 > n) continue;
            if (b1 < 1 || b1 > n || b2 < 1 || b2 > n) continue;
            if (g[a1][b1] != 'Q' || g[a2][b2] != '.') continue;
            // 改变
            g[a1][b1] = '.';
            g[a2][b2] = 'L';
            g[x][y] = '.';
            res = Math.max(res, dfs(a2, b2) + 1);
            // 还原现场
            g[a1][b1] = 'Q';
            g[a2][b2] = '.';
            g[x][y] = 'L';
        }

        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

